package com.loginlib.table.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.loginlib.R;
import com.loginlib.di.component.DaggerTableActivityComponent;
import com.loginlib.di.component.TableActivityComponent;
import com.loginlib.di.module.TableModule;
import com.loginlib.panel.TablePanel;
import com.loginlib.presenters.TablePresenter;
import com.loginlib.table.adapter.TableGridViewAdapter;
import com.wanhe.base.model.entity.TableResultEntity;

import javax.inject.Inject;


/**
 * Created by Administrator on 2016/7/25.
 */
public class TablePageView extends LinearLayout implements TablePanel{
    GridView gvTable;
    private TableGridViewAdapter mAdapter;
    @Inject
     TablePresenter mPresenter;
    private int pageID;
    TableActivityComponent tableActivityComponent;

    public TablePageView(Context context) {
        this(context, null);
    }

    public TablePageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.table_view, this, true);
        gvTable = (GridView) findViewById(R.id.gv_table);
        setOrientation(VERTICAL);
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initInject();
        initComponent();
        mPresenter.setTablePanel(this);

    }

    public void setPageID(int id){
        this.pageID = id;
        Log.d("TablePageView","pageID="+pageID);
        mPresenter.getTotalTableInfo(pageID);
    }

    private void initInject() {
        tableActivityComponent=  DaggerTableActivityComponent.builder().tableModule(new TableModule()).build();
        tableActivityComponent.inject(this);
    }

    private void initComponent() {
        mAdapter = new TableGridViewAdapter(getContext());
        gvTable.setAdapter(mAdapter);

    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mPresenter.onDestroy();
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void disLoading() {

    }


    @Override
    public void getTablesSuccess(TableResultEntity resultEntity) {
        mAdapter.setDatas(resultEntity.Tables);
    }
    @Override
    public void getTablesFail(String msg) {

    }
}
